const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    input_list: [],
    add_url: '',
    index_url: '',
    show_agreement: 0,
    box: 1
  },
  backto: function (e) {
    wx.navigateBack({})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    const token = extConfig.appid;
    app.ihttpGet("https://www.58zltc.com/api.php/Shop/getAddForm",function(res){
      var data = res.data;
      // var form = data.form;
     // console.log(data)
      that.setData({
        input_list: data.form,
        add_url: data.add_url,
        index_url: data.index_url,
        token: token
      })
    });
    wx.hideLoading();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getagree();
    console.log(this.data)
  },
  getagree(){
    var that = this;
    app.ihttpGet("https://www.58zltc.com/api.php/Shop/getShopAgreement/type/0", function (res) {
      var data = res.data;
      //var WxParse = require('../../wxParse/wxParse.js');
      var article = data.content;
      //WxParse.wxParse('article', 'html', article, that, 5);
      that.setData({
        shop_agreement_title: data.title,
        shop_agreement_content: data.content.replace(/\<img/gi, '<img style="max-width:100%;height:auto" ')
      })
    })
    wx.hideLoading();
  },
  bindChange: function (e) {
    // console.log(e);
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    list.forEach(function (item, idx) {
      if (item.title == data.title) {
        if(data.value == 'value1'){
          list[idx].value1 = e.detail.value;
        } else if (data.value == 'value2'){
          list[idx].value2 = e.detail.value;
        }else{
          list[idx].value = e.detail.value;
        }
      }
    })
    this.setData({
      input_list: list
    })
  },
  bindMultiPickerColumnChange: function(e){
    if(e.detail.column != 0){
      return false;
    }
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    list.forEach(function (item, idx) {
      if (item.title == data.title) {
        var index = e.detail.value;
        list[idx].range[1] = item.cates[index].sub_range;
        list[idx].value[0] = index;
      }
    })
    this.setData({
      input_list: list
    })
  },
  delImg: function (e) {
    const img_index = e.currentTarget.dataset.index;
    var list = this.data.input_list;
    var that = this;
    list.forEach(function (value, index) {
      if (value.name == 'pics') {
        var pics = list[index].pics;
        pics.splice(img_index, 1);
        list[index].pics = pics;
      }
    })
    that.setData({
      input_list: list
    })
  },
  bindUploadImg: function (e) {
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    var that = this;
    wx.chooseImage({
      count: data.num > 1 ? 9 : 1,
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.showLoading({
          title: '图片上传中……',
        })
        tempFilePaths.forEach(function (item, idx) {
          wx.uploadFile({
            url: 'https://www.58zltc.com/api.php/Index/uploadImage?token=' + that.data.token,
            filePath: item,
            name: 'file',
            success: function (res) {
              var out = JSON.parse(res.data);
              if (out.err_code > 0) {
                wx.showToast({
                  title: '上传失败！',
                })
              } else {
                list.forEach(function (value, index) {
                  if (value.title == data.title) {
                    var pics = list[index].pics;
                    var file_name = out.data.domain;
                    if (data.num > 1) {
                      pics.push(file_name);
                    } else {
                      pics = [file_name];
                    }
                    list[index].pics = pics;
                  }
                })
                that.setData({
                  input_list: list
                })
              }

              if (idx == tempFilePaths.length - 1) {
                wx.hideLoading();
              }
              //do something
            }
          })
        })
      }
    })
  },
  changebox:function(e){
    console.log(e.detail.value.length)
    if (e.detail.value.length > 0){
      var v = e.detail.value[0]
    }else{
      var v = 0
    }
    this.setData({
      box: v
    })
  },
  formSubmit: function (e) {
    if (this.data.box == 1) {
      app.addFormId(e.detail.formId);
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      var post_data = e.detail.value;
      delete post_data.check_box;
      var data = this.data;
      let that = this;
      data.input_list.forEach(function (item, idx) {
        if (item.type == 'picker1') {
          post_data[item.name] = item.range[item.value];
        } else if (item.type == 'picker2' || item.type == 'picker3') {
          post_data[item.name[0]] = item.range[0][item.value[0]];
          post_data[item.name[1]] = item.range[1][item.value[1]];
        } else if (item.type == 'date') {
          post_data[item.name] = item.value;
        } else if (item.type == 'time') {
          post_data[item.name] = item.value;
        } else if (item.type == 'time2') {
          post_data[item.name1] = item.value1;
          post_data[item.name2] = item.value2;
        } else if (item.type == 'tag') {
          var tags = '';
          item.tags.forEach(function (vo, key) {
            if (key == 0) {
              tags += vo.title;
            } else {
              tags += ',' + vo.title;
            }
          })
          post_data[item.name] = tags;
        } else if (item.type == 'pic') {
          post_data[item.name] = item.pics;
          // post_data.imgurl = item.pics[0];
        }
      })
      post_data.openid = app.globalData.openid;
      if (!data.latitude) {
        wx.showModal({
          title: '提示',
          content: '请在地图中选择店铺所在位置！',
          showCancel: false,
          confirmText: '马上选择',
          success: function (res) {
            wx.chooseLocation({
              success: function (res) {
                that.setData({
                  latitude: res.latitude,
                  longitude: res.longitude
                })
              }
            })
          }
        })
        wx.hideLoading();
        return false;
      }
      post_data.latitude = data.latitude;
      post_data.longitude = data.longitude;
      app.ihttpPost(data.add_url, post_data, function (res) {
        wx.hideLoading();
        if (res.data.err_code === 400) {
          wx.showModal({
            title: '提示',
            content: res.data.err_msg,
            showCancel: false
          })
        } else if (res.data.err_code === 0) {
          wx.showModal({
            title: '提示',
            content: res.data.err_msg,
            showCancel: false,
            success: function (res) {
              wx.redirectTo({
                url: data.index_url
              })
            }
          })
        } else {
          var data = res.data;
          var prepay_id = res.data.package;
          var a = prepay_id.slice(10);
          //支付添加formid
          app.ihttpPost("https://www.58zltc.com/api.php/Index/addFormId", { form_id: a, openid: app.globalData.openid, type: 'pay' }, function (res) { })
          wx.requestPayment({
            'appId': res.data.appId,
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonceStr,
            'package': res.data.package,
            'signType': 'MD5',
            'paySign': res.data.paySign,
            'success': res => {
              if (res.errMsg == 'requestPayment:ok') {

                wx.showModal({
                  title: '提示',
                  content: "支付成功！",
                  showCancel: false,
                  success: function (res) {
                    wx.redirectTo({
                      url: data.index_url
                    })
                  }
                })
              }
            }
          })
        }
      })
    } else {
      wx.showToast({
        title: '请阅读并了解协议内容!',
        icon: 'none',
        duration: 1500
      })
    }
  },
  show_agreement: function () {
    this.setData({
      show_agreement: 1
    })
  },
  hiden_agreement: function () {
    this.setData({
      show_agreement: 0
    })
  }
})